package com.jzo2o.orders.manager.service.client;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.jzo2o.api.foundations.ServeApi;
import com.jzo2o.api.foundations.dto.response.ServeAggregationResDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author ghy
 * @version V1.0
 * @date 2025-04-01 16:34
 */
@Component
@Slf4j
public class FoundationsClient {

    @Resource
    private ServeApi serveApi;

    @SentinelResource(value = "serveDetail", fallback = "serveDetailFallback", blockHandler = "serveDetailBlockHandler")
    public ServeAggregationResDTO findById(Long serveId){
        return serveApi.findById(serveId);
    }

    /**
     * 降级方法
     * @param serveId
     * @param t
     * @return
     */
    public ServeAggregationResDTO serveDetailFallback(Long serveId, Throwable t){
        log.error("根据ID查询服务信息方法执行失败，降级了:{}", t.getMessage());
        ServeAggregationResDTO dto = new ServeAggregationResDTO();
        dto.setId(-1L);
        return dto;
    }

    /**
     * 熔断方法
     * @param serveId
     * @param blockException
     * @return
     */
    public ServeAggregationResDTO serveDetailBlockHandler(Long serveId, BlockException blockException){
        log.error("根据ID查询服务信息方法执行失败，熔断了:{}", blockException.getMessage());
        ServeAggregationResDTO dto = new ServeAggregationResDTO();
        dto.setId(-2L);
        return dto;
    }

}
